import time
import logging
import configparser
import paho.mqtt.client as mqtt


def on_connect(client, userdata, flags, rc):
    print(f"Connected with result code {rc}")


if __name__ == '__main__':
    logging.basicConfig(filename='log_publisher.log', level=logging.INFO)
    config = configparser.ConfigParser()
    config.read('properties.conf')
    mqtt_config = config['mqtt']

    client = mqtt.Client("tem_publisher")
    client.on_connect = on_connect
    host = mqtt_config['host']
    client.connect(host)
    topic = "temperature"
    for i in range(1, 300_0000):
        # if client.is_connected():
        if i % 200 == 0:
            print(">>>> sleep 1s")
            time.sleep(1)
        print(">>>> {}:{}".format(topic, i))
        client.publish(topic, payload=i * i, qos=0, retain=False)
        # else:
        #     print(">>>> is not connected")
        #     client.reconnect()
        #     time.sleep(3)
    client.loop_forever()